# 通过队列完成进程间通讯
import multiprocessing


def download_data(q):
    """模拟下载数据"""
    data = [1, 2, 3, 4]

    # 往队列添加数据
    for temp in data:
        q.put(temp)

    print("数据写入队列成功")


def handle_data(q):
    # 定义集合存储数据
    data_list = list()
    while not q.empty():
        data_list.append(q.get())

    print(data_list)


def main():
    # 可以指定队列数，如果不指定则根据硬件自动计算
    q = multiprocessing.Queue()
    p1 = multiprocessing.Process(target=download_data, args=(q,))
    p2 = multiprocessing.Process(target=handle_data, args=(q,))

    p1.start()
    p2.start()


if __name__ == '__main__':
    main()
